package kr.saeildadim.androidexam_saeildamil.chapter13;

import java.util.Calendar;
import java.util.GregorianCalendar;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class Ch13_PickerDialog extends Activity {
	int mYear;
	int mMonth;
	int mDay;
	int mHour;
	int mMinuit;
	TextView mDatetv;
	TextView mTimetv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch13_pickerdialog);
	
	mDatetv= (TextView) findViewById(R.id.ch13_pickerdialog_textdate);
	mTimetv= (TextView) findViewById(R.id.ch13_pickerdialog_texttime);
	
	Calendar cal = new GregorianCalendar();
	mYear = cal.get(Calendar.YEAR);
	mMonth = cal.get(Calendar.MONTH);
	mDay= cal.get(Calendar.DAY_OF_MONTH);
	mHour = cal.get(Calendar.HOUR_OF_DAY);
	mMinuit= cal.get(Calendar.MINUTE);
	
	updateNow();
	
		
	}

	private void updateNow() {
		mDatetv.setText(String.format("%d/%d/%d", mYear,mMonth+1,mDay));
		mTimetv.setText(String.format("%d:%d", mHour,mMinuit));
		
	}

	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.ch13_pickerdialog_changedate_btn:
			new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay).show();
			break;
		
		case R.id.ch13_pickerdialog_changetime_btn:
			new TimePickerDialog(this, mTimeSetListener, mHour, mMinuit, false).show();
			break;	
		}
	}

	DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
				mYear= year;
				mMonth= monthOfYear;
				mDay=dayOfMonth;
				updateNow();
			
		}
	};
	
	TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour =hourOfDay;
			mMinuit = minute;
			updateNow();
			
		}
	};


}
